package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        //strategyService.saveOrUpdate(strategy);
        //目的地
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //作者 //暂时没有
        //摘要
        //1,由运营人员来处理
        //2,程序员,截取你明细内容前200个字
        String content = strategyDetail.getStrategyContent().getContent();
        if (content.length() > 200) {
            strategyDetail.setSummary(content.substring(0, 200));
        } else {
            strategyDetail.setSummary(content);
        }
        //国外/国内  只有地区是1的才是国家,其他都是国外,那么可以通过面包屑获取第一个就是最大的国家来判断是国内还是国外
        List<Destination> toasts = destinationService.getToasts(strategyDetail.getDest().getId());
        //约定了国内是false,国外是true
        strategyDetail.setIsabroad(toasts.get(0).getId() != 1);
        //明细内容
        if (strategyDetail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //明细内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
            //在修改标签之前先删除关系//打破关系
            strategyDetailMapper.deleteRelation(strategyDetail.getId());
        } else {
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            //明细内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }
        //标签的保存
        if (StringUtils.hasLength(tags)) {
            String[] tagArr = tags.split(",");
            if (tagArr.length > 0) {
                for (String tag : tagArr) {
                    StrategyTag strategyTag = new StrategyTag();
                    strategyTag.setName(tag);
                    strategyTagService.saveOrUpdate(strategyTag);
                    //维护关系//中间表的关系
                    strategyDetailMapper.insertRelation(strategyTag.getId(), strategyDetail.getId());
                }
            }
        }
    }

    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public List<StrategyDetail> getCatalogsViemnumTop3(Long id) {
        return strategyContentMapper.queryCatalogsViemnumTop3(id);
    }

    public void updateStatisVO(StrategyStatisPersistenceVO vo) {
        strategyDetailMapper.updateStatisVO(vo);
    }
}
